/**
 * JAVACC DEMO 1.0
 * @copy right dwusoft company All rights reserved. 
 * @Package com.apache.audit.aop  
 */
package com.apache.client;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.aop.AfterReturningAdvice;

import com.apache.api.vo.ParamsVo;
import com.apache.method.ThreadPool;

/**
 * description:  方法执行后的日志通知
 * @author Hou Dayu 创建时间：2014-8-7  
 * @Copyright(c)2014:北京--软件技术有限公司
 */
public class LogClientAfterAdvice implements AfterReturningAdvice {

	private Logger log = Logger.getLogger(LogClientAfterAdvice.class);

	/**
	 * description:  组装日志对象信息
	 * @param userEname 操作人账号
	 * @param userId    操作人ID
	 * @param methodName 当前操作的方法
	 * @return   返回日志对象信息
	 * @author Hou Dayu  
	 * @update 2014-8-7
	 */
	private Map<String, Object> getAuditLog(Object returnValue, String userEname, String userId, String methodName) {
		Map<String, Object> result = new HashMap<String, Object>();
		if (!isNull(userEname)) {
			result.put("log_userId", userId);
			result.put("log_userEname", userEname);
			if (methodName.startsWith("save")) {
				result.put("logType", "save");
			} else if (methodName.startsWith("edit")) {
				result.put("logType", "edit");
			} else if (methodName.startsWith("delete")) {
				result.put("logType", "del");
			}
			result.put("log_returnValue", String.valueOf(returnValue));
		}
		return result;
	}

	/**
	 * TODO 实现方法后日志拦截. 
	 * @see org.springframework.aop.AfterReturningAdvice#afterReturning(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], java.lang.Object)
	 */
	@SuppressWarnings("rawtypes")
	public void afterReturning(Object returnValue, Method method, Object[] args, Object target) throws Throwable {
		String targetName = target.getClass().getName();
		String methodName = method.getName();

		if (targetName.indexOf("com.apache.audit") == -1) {
			if (args.length > 0) {
				try {
					ParamsVo arguments = (ParamsVo) args[0];
					Map<String, Object> map = getAuditLog(returnValue, arguments.getUserEname(), arguments.getUserId(),
							methodName);
					if (null != map && !map.isEmpty()) {
						if (!isNull(arguments.getInfoId())) {
							map.put("infoId", arguments.getInfoId());
						}
						if (isNull(String.valueOf(map.get("logType")))) {
							if (!isNull(String.valueOf(arguments.getParams("logType")))) {
								map.put("logType", arguments.getParams("logType"));
								map.put("log_returnValue", "true");
							}
						}
						map.put("logClassName", arguments.getLogClassName().trim());
						map.put("logIp", arguments.getParams("logIp"));
						LoggerClientRunnable runnable = new LoggerClientRunnable(arguments.getObj(), map);
						ThreadPool.newFixedThreadPool(1).execute(runnable);
						//ScheduledExecutor.newInstance().saveLogger(runnable);
						//new Thread(runnable).start();
					}
				} catch (Throwable e) {
					log.error(e);
				}
			}
		}
	}

	/**
	 *判断对象数组是否为空
	 * 
	 * @param  object 对象数组
	 * @return        如果object为空返回true，不为空返回false 
	 */
	private boolean isNull(String str) {
		if (str == null) {
			return true;
		}
		str = str.trim();
		if ((str.equalsIgnoreCase("null")) || (str.equals(""))) {
			return true;
		}
		return false;
	}
}
